#include <iostream>
#include <cstring>
#include <stack>

const int maxn = 500 + 10;

typedef struct node {
    char ch;
    struct node *lchild, *rchild;
} node;

node *arr[30];
char str[maxn];

node *get_new_node(char ch) {
    node *p = (node*)malloc(sizeof(node));
    p->ch = ch;
    p->lchild = p->rchild = nullptr;
    return p;
}

void output(node *root) {
    if (root == nullptr) return;
    printf("%c", root->ch);
    if (root->lchild == nullptr && root->rchild == nullptr) return ;
    printf("(");
    output(root->lchild);
    if (root->rchild) {
        printf(",");
    }
    output(root->rchild);
    printf(")");
    return ;
}

int main() {
    node *root = nullptr, *p;

    while (~scanf("%s", str)) {
        if (str[0] == '^' && str[1] == '^')  {
            break;
        }
        p = get_new_node(str[1]);
        arr[str[1] - 'A'] = p; // C是按照层序出现的,我们必须是去覆盖之前的字母
        if (str[0] == '^') {
            root = p;
            continue;
        }
        switch (str[2]) {
            case 'L': arr[str[0] - 'A']->lchild = p; break;
            case 'R': arr[str[0] - 'A']->rchild = p; break;
        }
    }

    output(root);

    return 0;
}